package com.example.a2306_fx.detail

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Bundle
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.ActionBar.LayoutParams
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.a2306_fx.R
import com.example.a2306_fx.databinding.ActivityDetailBinding
import com.example.base.mvvm.BaseActivity

@Suppress("DEPRECATION")
@Route(path = "/app/DetailActivity")
class DetailActivity : BaseActivity<ActivityDetailBinding,DetailViewModel>() {
    @Autowired
    @JvmField
    var pid = 1

    var imgs = mutableListOf<String>()
    override fun getLayout(): Int  = R.layout.activity_detail

    override fun getViewModel(): Class<DetailViewModel>  = DetailViewModel::class.java

    override fun initView() {
       ARouter.getInstance().inject(this)

        viewModel.detail(pid)

        binding.btnAddcar.setOnClickListener {
            viewModel.cartAdd(1,pid.toString(),pid)
        }
    }

    override fun initData() {
       viewModel.detailSuccess.observe(this){
           if(it.code==200){
               binding.info = it.data.productInfo
               imgs.add(it.data.productInfo.image)
               imgs.add(it.data.productInfo.image)
               binding.imgs = imgs
           }
       }
        viewModel.addSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                //展示动画
                val imageView = ImageView(this)
                val layoutParams = LayoutParams(100, 100)
                imageView.layoutParams = layoutParams
                Glide.with(this).load(imgs[0]).into(imageView)
                //添加到页面上
                binding.main.addView(imageView)
                //设置动画效果
                val width = windowManager.defaultDisplay.width.toFloat()
                val height = windowManager.defaultDisplay.height.toFloat()

                val translationX = ObjectAnimator.ofFloat(imageView, "translationX", 0f, width, 100f)
                val translationY = ObjectAnimator.ofFloat(imageView, "translationY", 0f, height)

                val animatorSet = AnimatorSet()
                animatorSet.duration = 2000
                animatorSet.play(translationX).with(translationY)
                animatorSet.start()

                animatorSet.addListener(object:Animator.AnimatorListener{
                    override fun onAnimationStart(animation: Animator) {
                    }

                    override fun onAnimationEnd(animation: Animator) {
                        binding.main.removeView(imageView)
                    }

                    override fun onAnimationCancel(animation: Animator) {
                    }

                    override fun onAnimationRepeat(animation: Animator) {
                    }
                })
            }
        }
    }

}